package com.clientmanage.service.impl;

import com.clientmanage.domain.bean.Customer;
import com.clientmanage.mapper.CustomerMapper;
import com.clientmanage.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.clientmanage.domain.vo.clientInfoVo;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.clientmanage.domain.bean.Pet;
import com.clientmanage.mapper.PetMapper;
import com.clientmanage.domain.bean.PetVaccineRecord;
import com.clientmanage.mapper.PetVaccineRecordMapper;
import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HUANG
 * @since 2025-06-30
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetVaccineRecordMapper petVaccineRecordMapper;

    @Override
    public List<clientInfoVo> getClientInfoList(String name, String phone, String petName, String petBreed, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return customerMapper.selectClientInfoList(name, phone, petName, petBreed, pageSize, offset);
    }

    @Transactional
    @Override
    public void saveOrUpdateClientInfo(clientInfoVo vo) {
        if (vo.getId() == null) {
            // 新增客户
            Customer customer = new Customer();
            customer.setName(vo.getName());
            customer.setPhone(vo.getPhone());
            customer.setAddress(vo.getAddress());
            customer.setIsMember(false);
            customer.setMemberLevel("普通会员");
            customer.setPointsBalance(0);
            customer.setMemberStatus("正常");
            customerMapper.insert(customer);
        } else {
            // 更新客户
            Customer customer = customerMapper.selectById(vo.getId());
            if (customer != null) {
                customer.setName(vo.getName());
                customer.setPhone(vo.getPhone());
                customer.setAddress(vo.getAddress());
                customerMapper.updateById(customer);
            }
        }
    }
    
    @Override
    public void deleteCustomerById(String id) {
        customerMapper.deleteById(id);
    }
    
    @Transactional
    @Override
    public void addPet(clientInfoVo vo) {
        Pet pet = new Pet();
        pet.setCustomerId(Long.valueOf(vo.getCustomerId()));
        pet.setName(vo.getPetName() != null ? vo.getPetName() : vo.getName());
        pet.setBreed(vo.getBreed());
        pet.setGender(vo.getGender());
        pet.setAgeMonth(vo.getAgeMonth());
        pet.setWeightKg(vo.getWeightKg());
        pet.setHealthStatus(vo.getHealthStatus());
        petMapper.insert(pet);
    }
    
    @Transactional
    @Override
    public void updatePet(clientInfoVo vo) {
        Pet pet = petMapper.selectById(Long.valueOf(vo.getPetId()));
        if (pet != null) {
            pet.setName(vo.getPetName());
            pet.setBreed(vo.getBreed());
            pet.setGender(vo.getGender());
            pet.setAgeMonth(vo.getAgeMonth());
            pet.setWeightKg(vo.getWeightKg());
            pet.setHealthStatus(vo.getHealthStatus());
            petMapper.updateById(pet);
        }
    }
    
    @Override
    public void deletePetById(String id) {
        petMapper.deleteById(Long.valueOf(id));
    }
    
    @Transactional
    @Override
    public void addVaccine(clientInfoVo vo) {
        PetVaccineRecord record = new PetVaccineRecord();
        record.setPetId(Long.valueOf(vo.getPetId()));
        record.setVaccineName(vo.getVaccineName());
        record.setBatch(vo.getBatch());
        record.setDate(vo.getDate());
        petVaccineRecordMapper.insert(record);
    }
    
    @Transactional
    @Override
    public void updateVaccine(clientInfoVo vo) {
        PetVaccineRecord record = petVaccineRecordMapper.selectById(String.valueOf(vo.getId()));
        if (record != null) {
            record.setVaccineName(vo.getVaccineName());
            record.setBatch(vo.getBatch());
            record.setDate(vo.getDate());
            petVaccineRecordMapper.updateById(record);
        }
    }
    
    @Override
    public void deleteVaccineById(String id) {
        petVaccineRecordMapper.deleteById(Long.valueOf(id));
    }
    
    @Transactional
    @Override
    public void upgradeToMember(String customerId, String memberLevel) {
        Customer customer = this.getById(customerId);
        if (customer != null) {
            customer.setIsMember(true);
            customer.setMemberLevel(memberLevel);
            customer.setPointsBalance(0);
            customer.setMemberStatus("正常");
            customer.setLastConsumeDate(java.time.LocalDate.now());
            customer.setUpdatedAt(java.time.LocalDateTime.now());
            this.updateById(customer);
        }
    }
}
